package com.example.sneakerappforme1105;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bumptech.glide.Glide;
import com.example.sneakerappforme1105.ViewModel.CartViewModel;
import com.example.sneakerappforme1105.adapter.SneakerPagerAdapter;
import com.example.sneakerappforme1105.bean.Cart;
import com.example.sneakerappforme1105.bean.CartItem;
import com.example.sneakerappforme1105.bean.Goods;
import com.example.sneakerappforme1105.dao.CartDao;
import com.example.sneakerappforme1105.dao.CartItemDao;
import com.example.sneakerappforme1105.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RootActivity extends AppCompatActivity implements DataProvider{
    private ViewPager vp_content;
    private RadioGroup rg_tabbar;
    private Fragment fg_home;
    private Fragment fg_cart;
    private Fragment fg_my;
    private FragmentManager fManager;
    private static long mUserId;   // 用户id
    private static Long mCartId;   // 购物车id
    private CartDao cartDao;
    private CartItemDao cartItemDao;
    private String TAG = "RootActivity";
    private CartViewModel cartViewModel;
    private int fragmentIdx = 0;
    private List<CartItem> mCartItemList = new ArrayList<>();
    private HashMap<Long,Goods> mGoodsMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_root);

        cartDao = MainApplication.getInstance().getAppDatabase().cartDao();
        cartItemDao = MainApplication.getInstance().getAppDatabase().cartItemDao();

        mUserId = getIntent().getLongExtra("userId", 0L);
        mCartId = getIntent().getLongExtra("cartId", 0L);   // 得让goodsDetail也传回来
        Log.d(TAG, "购物车Id为"+mCartId);
        Log.d(TAG, "用户Id为"+mUserId);



        // 判断接收的片段索引是不是
        fragmentIdx = 0; // 初始话为0
        if(getIntent().getIntExtra("cartFragment",0)==1) {
            fragmentIdx = 1;
        }else if(getIntent().getIntExtra("MyFragment",0)==2){
            fragmentIdx = 2;
        }

        //设置按钮
        // 11.16
        rg_tabbar = findViewById(R.id.rg_tabbar);
        ((RadioButton)rg_tabbar.getChildAt(fragmentIdx)).setChecked(true);




        vp_content = findViewById(R.id.vp_content); // viewPager
        SneakerPagerAdapter adapter = new SneakerPagerAdapter(getSupportFragmentManager());
        vp_content.setAdapter(adapter);


        vp_content.setCurrentItem(fragmentIdx); // 11.16

        // 设置翻页监听
        vp_content.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                rg_tabbar.check(rg_tabbar.getChildAt(position).getId());
            }
        });


        // 设置单选组的选中监听器
        rg_tabbar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                for(int pos=0; pos<rg_tabbar.getChildCount(); pos++){
                    // 获得指定位置的单选按钮
                    RadioButton tab = (RadioButton) rg_tabbar.getChildAt(pos);
                    if(tab.getId() == checkedId){   // 来自参数
                        vp_content.setCurrentItem(pos);
                    }
                }




            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"应用程序暂停");


    }

    @Override
    protected void onResume() { // 来自 GoodsActivity
        super.onResume();
        cartViewModel = MainApplication.getInstance().getCartViewModel();

        cartViewModel.getCartItemList().observe(this, items -> {
            mCartItemList = items != null ? items : new ArrayList<>();

        });

        cartViewModel.getGoodsMap().observe(this, map->{
            mGoodsMap = map !=null ? map: new HashMap<>();
        });
//        cartViewModel.updateCartItems(mCartItemList);
        Log.d(TAG,"Root重新启动"+mCartItemList.size());


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"Root活动结束");
    }

    @Override
    public Long getUserId() {
        return mUserId;
    }

    @Override
    public Long getCartId() {
        return mCartId;
    }

    @Override
    public CartViewModel getCartViewModel() {
        return cartViewModel;
    }

    @Override
    public List<CartItem> getCartItemList() {
        return mCartItemList;
    }

    @Override
    public HashMap<Long, Goods> getGoodsMap() {
        return mGoodsMap;
    }
}